package com.ztgf.sys.service;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.message.model.bo.MessageAnnouncementSaveBO;
import com.ztgf.message.model.constant.MessageTopicType;
import com.ztgf.message.model.dto.ResponseDTO;
import com.ztgf.message.model.enums.MessageEnum;
import com.ztgf.message.model.service.IKafkaProducerService;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sys.common.config.YmlConfig;
import com.ztgf.sys.mapper.SysComplaintsMapper;
import com.ztgf.sys.models.bo.SysComplaintsSaveBO;
import com.ztgf.sys.models.dto.SysComplaintsDTO;
import com.ztgf.sys.models.service.ISysComplaintsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;


/**
 * 投诉意见表控制器
 *
 * @author YUE
 * @Date 2020-05-27 10:55:58
 */
@DubboService
public class SysComplaintsServiceImpl extends ServiceImpl<SysComplaintsMapper, SysComplaintsDTO> implements ISysComplaintsService {

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IKafkaProducerService kafkaProducerService;

    @DubboReference
    private IShopInfoService shopInfoService;

    @Override
    public boolean add(SysComplaintsSaveBO saveBO) {
        SysComplaintsDTO dto = new SysComplaintsDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long shopId = saveBO.getShopId();
        ShopInfoDTO shopInfoDTO = shopInfoService.selectById(shopId);
        dto.setShopName(shopInfoDTO.getName());
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return insert(dto);
    }

    @Override
    public Result processComplaints(Long id, String processStr) {
        SysComplaintsDTO sysComplaintsDTO = selectById(id);
        sysComplaintsDTO.setResult(processStr);
        sysComplaintsDTO.setState(1);
        updateById(sysComplaintsDTO);
        //发送消息
        ResponseDTO responseDTO = new ResponseDTO();
        MessageAnnouncementSaveBO messageAnnouncementSaveBO = new MessageAnnouncementSaveBO();
        messageAnnouncementSaveBO.setTitle("投诉建议");
        messageAnnouncementSaveBO.setContent(sysComplaintsDTO.getContent());
        messageAnnouncementSaveBO.setShopId(sysComplaintsDTO.getShopId());
        messageAnnouncementSaveBO.setType(MessageEnum.SERVER_MESSAGE.getCode());
        messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_1.getCode());
        messageAnnouncementSaveBO.setJumpType(MessageEnum.COMPLAINTS_MESSAGE.getCode() + "");
        messageAnnouncementSaveBO.setTextType(1);
        messageAnnouncementSaveBO.setKey(sysComplaintsDTO.getShopId()+"");
        responseDTO.setData(messageAnnouncementSaveBO);
        responseDTO.setMessageId(sysComplaintsDTO.getShopId() + "");
        kafkaProducerService.senMessage(JSON.toJSONString(responseDTO), MessageTopicType.COMMENT_MESSAGE_TOPIC);

        return new Result();
    }

}
